package com.example.client2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.example.ipcdemo.pool.INums;
import com.example.ipcdemo.pool.ISay;

public class PoolTestActivity extends AppCompatActivity {
    private static final String TAG = "PoolTestActivity";
    private BinderPool mInstance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pool_test);

        mInstance = BinderPool.getInstance(this);

        new Thread(new Runnable() {
            @Override
            public void run() {
                mInstance.connectService();

            }
        }).start();

    }


    public void say(View view) {
        IBinder binder = mInstance.queryBinder(BinderPool.BINDER_SAY);
        ISay iSay = ISay.Stub.asInterface(binder);
        if (iSay != null) {
            try {
                iSay.speak("我是测试1");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

    }

    public void num(View view) {
        IBinder binder = mInstance.queryBinder(BinderPool.BINDER_NUM);
        INums nums = INums.Stub.asInterface(binder);
        if (nums != null) {
            try {
                int num = nums.add(2,3);
                Log.d(TAG, "zsr num: "+num);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mInstance.onRelease();
    }
}